﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PCR.Base;
using PCR.Logic;

namespace PCR
{
    public partial class FormUserReportInfo : DevComponents.DotNetBar.Office2007Form
    {
        private bool _isAddMode = true;
        public IPatientReport _report = null;

        public FormUserReportInfo(bool isAddMode, IPatientReport report)
        {
            InitializeComponent();

            this.AcceptButton = this.btnOK;
            this.CancelButton = this.btnCancel;

            {
                this.cbCheckMethod.Items.Clear();
                this.cbCheckMethod.Items.Add(PCRRoot.CheckMethodString(CheckMethod.HPV));
                this.cbCheckMethod.Items.Add(PCRRoot.CheckMethodString(CheckMethod.OTHER));
                this.cbCheckMethod.SelectedIndex = 0;

                this.cbSampleType.Items.Clear();
                this.cbSampleType.Items.Add(PCRRoot.SampleTypeString(SampleType.NECKUTERUS));
                this.cbSampleType.Items.Add(PCRRoot.SampleTypeString(SampleType.SKIN));
                this.cbSampleType.Items.Add(PCRRoot.SampleTypeString(SampleType.WEASAND));
                this.cbSampleType.Items.Add(PCRRoot.SampleTypeString(SampleType.OTHER));
                this.cbSampleType.SelectedIndex = 0;

                this.cbSampleState.Items.Clear();
                this.cbSampleState.Items.Add(PCRRoot.SampleStateString(SampleState.NOTWELL));
                this.cbSampleState.Items.Add(PCRRoot.SampleStateString(SampleState.WELL));
                this.cbSampleState.Items.Add(PCRRoot.SampleStateString(SampleState.UNKNOWN));
                this.cbSampleState.SelectedIndex = 0;
            }

            this._isAddMode = isAddMode;
            string strMode = "";
            if (this._isAddMode)
            {
                strMode = "添加";
                this.dgvVirusCT.Rows.Clear();
                IVirusTypeManager virusTypeMgr = PCRRoot.Instance.VirusTypeManager;
                if (virusTypeMgr != null)
                {
                    foreach (IVirusType type in virusTypeMgr.List)
                    {
                        //this.VirusType.Items.Add(type.Name);
                        this.dgvVirusCT.Rows.Add(false,type.Name, 0, 0);
                    }
                }
            }
            else
            {
                strMode = "编辑";
                _report = report;
                this.dtReportDate.Value = report.ReportDate;
                this.tbReportID.Text = report.ReportID;
                this.tbName.Text = report.Name;
                this.tbAge.Text = report.Age.ToString();
                this.cbGender.Text = PCRRoot.GenderString(report.Gender);
                this.dtCheckDate.Value = report.CheckDate;
                this.tbSampleID.Text = report.SampleID;
                this.dtSampleReceiveDate.Value = report.SampleReceiveDate;
                this.cbCheckMethod.Text = PCRRoot.CheckMethodString(report.CheckMethod);
                this.cbSampleType.Text = PCRRoot.SampleTypeString(report.SampleType);
                this.cbSampleState.Text = PCRRoot.SampleStateString(report.SampleState);
                this.rtbCellPrinciple.Text = report.CellPrinciple;
                this.rtbPathologyPrinciple.Text = report.PathologyPrinciple;

                foreach(IVirusType type in PCRRoot.Instance.VirusTypeManager.List)
                {
                    bool bAdd = false;
                    foreach (IPatientVirus virus in report.PatientVirusList)
                    {
                        if (type.ObjectID == virus.VirusTypeID)
                        {
                            bAdd = true;
                            this.dgvVirusCT.Rows.Add(true, type.Name, virus.VirusCTVal, virus.ReferCellCTVal);
                            break;
                        }
                    }

                    if (!bAdd)
                    {
                        this.dgvVirusCT.Rows.Add(false, type.Name, 0, 0);
                    }
                }

            }

            this.Text = strMode + this.Text;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (this.tbReportID.Text.Length == 0)
            {
                MessageBox.Show("报告编号不能为空!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.tbReportID.Focus();
                return;
            }

            if (this.tbName.Text.Length == 0)
            {
                MessageBox.Show("姓名不能为空!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.tbName.Focus();
                return;
            }


            UInt32 uiAge = 0;
            bool bAge = UInt32.TryParse(this.tbAge.Text, out uiAge);            
            if (!bAge)
            {
                MessageBox.Show("年龄输入有误!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.tbAge.Focus();
                return;
            }

            if (this.tbSampleID.Text.Length == 0)
            {
                MessageBox.Show("样本编号不能为空!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.tbSampleID.Focus();
                return;
            }
            
            IPatientReport report = PCRRoot.Instance.CreateObject(ObjectType.PatientReport) as IPatientReport;
            report.ReportDate = DateTime.Parse(this.dtReportDate.Text);
            report.ReportID = this.tbReportID.Text;
            report.Name = this.tbName.Text;
            report.Age = uiAge;
            report.Gender = PCRRoot.GenderValue(this.cbGender.Text);
            report.CheckDate = DateTime.Parse(this.dtCheckDate.Text);
            report.SampleID = this.tbSampleID.Text;
            report.SampleReceiveDate = DateTime.Parse(this.dtSampleReceiveDate.Text);
            report.CheckMethod = PCRRoot.CheckMethodValue(this.cbCheckMethod.Text);
            report.SampleType = PCRRoot.SampleTypeValue(this.cbSampleType.Text);
            report.SampleState = PCRRoot.SampleStateValue(this.cbSampleState.Text);

            int i = 0;
            foreach (DataGridViewRow row in this.dgvVirusCT.Rows)
            {
                i++; 
                if ((i == this.dgvVirusCT.Rows.Count) || !(bool.Parse(row.Cells[0].Value.ToString()))) 
                    continue;

                double dCTVal = 0;
                if (!double.TryParse(row.Cells[2].Value.ToString(), out dCTVal) 
                    || dCTVal > PCRRoot.Instance.ConfigManager.MaxCTVal 
                    || dCTVal <= PCRRoot.Instance.ConfigManager.MinCTVal)
                {
                    MessageBox.Show("CT值输入有误!\n1.类型不对\n2.过大或者过小", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                double dCTCellVal = 0;
                if (!double.TryParse(row.Cells[3].Value.ToString(), out dCTCellVal)
                    || dCTCellVal > PCRRoot.Instance.ConfigManager.MaxCTVal
                    || dCTCellVal <= PCRRoot.Instance.ConfigManager.MinCTVal)
                {
                    MessageBox.Show("CT值输入有误!\n1.类型不对\n2.过大或者过小", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                IPatientVirus virus = PCRRoot.Instance.CreateObject(ObjectType.PatientVirus) as IPatientVirus;
                virus.VirusTypeID = PCRRoot.VirusTypeValue(row.Cells[1].Value.ToString()).ObjectID;
                virus.VirusCTVal = dCTVal;
                virus.ReferCellCTVal = dCTCellVal;
                report.PatientVirusList.Add(virus);
            }

            report.CellPrinciple = this.rtbCellPrinciple.Text;
            report.PathologyPrinciple = this.rtbPathologyPrinciple.Text;
            if (_isAddMode)
            {
                _report = report;
                PCRRoot.Instance.PatientManager.Add(report);
            }
            else
            {
                PCRRoot.Instance.PatientManager.RemoveByID(_report.ObjectID);
                _report = report;
                PCRRoot.Instance.PatientManager.Add(report);
            }
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {

        }
    }
}
